package com.example.onlineeducation.ui.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseDialogFragment
import com.example.onlineeducation.databinding.CertificateDialogFragmentBinding
import com.yc.jetpacklib.extension.ycIsEmpty
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.extension.ycLoadImageResCircleCenterInside

class CertificateDialogFragment : BaseDialogFragment<CertificateDialogFragmentBinding>(CertificateDialogFragmentBinding::inflate) {

    private var mContentHtmlCode: String = ""
    private var mSealImgUrl: String? = null

    override fun CertificateDialogFragmentBinding.initView(view: View, savedInstanceState: Bundle?) {
        ivCertificateBg.ycLoadImageResCircleCenterInside(R.drawable.ic_certificate_bg, 15)
        ivClose.setOnClickListener {
            dismiss()
        }
        initWebView()
        if (mSealImgUrl.ycIsNotEmpty()) {
            ivSeal.ycLoadImageNet(mSealImgUrl)
            ivSeal.isVisible = true
        } else {
            ivSeal.isVisible = false
        }
        webCertificateContent.loadData(mContentHtmlCode, "text/html", "utf-8")
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun CertificateDialogFragmentBinding.initWebView() {
        webCertificateContent.setBackgroundColor(0)
        webCertificateContent.isVerticalScrollBarEnabled = false //纵轴滚动条不显示
        val settings = webCertificateContent.settings //WebView配置管理
        settings.javaScriptEnabled = true //支持JS
        settings.setSupportZoom(false) //设置可以支持缩放
        settings.textZoom = 80
        settings.builtInZoomControls = false //设置内置的缩放控件。若为false，则该WebView不可缩放
        settings.displayZoomControls = false //隐藏原生的缩放控件
        settings.loadWithOverviewMode = false //缩放至屏幕的大小，使用预览模式加载界面。
        settings.loadsImagesAutomatically = true //支持自动加载图片
        settings.domStorageEnabled = true //设置DOM Storage缓存 ,https页面加载不出来就用它;
        settings.allowFileAccess = true //允许访问文件
    }

    fun setImgUrl(contentHtmlCode: String, sealImgUrl: String? = null) {
        mContentHtmlCode = contentHtmlCode
        mSealImgUrl = sealImgUrl
    }
}